using System;
using System.IO;
using Godot;
using ProtoBuf;

namespace Arcane.Source.Data
{
    public struct RushModeData
    {
        public string name;
        public StringName enemySequenceId;
        public int seqIndex;
        public GameStage stage;
        public PlayerData player;
        public ulong rngSeed;
        public ulong rngState;

        public static RushModeData Load(Save save)
        {
            return new()
            {
                name = save.name,
                stage = save.stage,
                enemySequenceId = save.enemySequenceId,
                seqIndex = save.seqIndex,
                rngSeed = save.rngSeed,
                rngState = save.rngState,
                player = PlayerData.Load(save.player),
            };
        }

        public Save GetSave() => new()
        {
            name = name,
            stage = stage,
            enemySequenceId = enemySequenceId,
            seqIndex = seqIndex,
            rngSeed = rngSeed,
            rngState = rngState,
            player = player.GetSave(),
        };

        [ProtoContract]
        public struct Save
        {
            [ProtoMember(1)]
            public string name;
            [ProtoMember(2)]
            public ProtoBuf.WellKnownTypes.Timestamp timestamp;
            [ProtoMember(3)]
            public GameStage stage;
            [ProtoMember(4)]
            public string enemySequenceId;
            [ProtoMember(5)]
            public int seqIndex;
            [ProtoMember(6)]
            public ulong rngSeed;
            [ProtoMember(7)]
            public ulong rngState;
            [ProtoMember(8)]
            public PlayerData.Save player;

            public static Save FromStream(Stream stream)
            {
                return Serializer.Deserialize<Save>(stream);
            }

            public static Save FromBytes(byte[] bytes)
            {
                using var mem = new MemoryStream(bytes);
                return FromStream(mem);
            }
        }
    }
}